home *** CD-ROM | disk | FTP | other *** search
/ POINT Software Programming / PPROG1.ISO / pascal / swag / textfile.swg / 0048_Text File Paginator.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1995-02-28  |  5.0 KB  |  158 lines

  1. PROGRAM PaginateTextFiles;
  2. CONST
  3.      ProgData = 'PAGINATE- Free DOS utility: text file paginator.';
  4.      ProgDat2 = 'V1.00: July 14, 1993. (c) 1993 by David Daniel Anderson - Reign Ware.';
  5.      Usage   = 'Usage:   PAGINATE /i<infile> /o<outfile> /l##[:##] (lines per odd/even page)';
  6.      Example = 'Example: PAGINATE /iNOTPAGED.TXT /oPAGED.TXT /l55';
  7. VAR
  8.      IFL, OFL, LPP    : String[80];
  9.      InFile, OutFile  : Text;
  10.      OddL,EvenL       : Word;
  11.  
  12. PROCEDURE InValidParms( ErrCode : Word );
  13. VAR
  14.    Message : String[80];
  15. BEGIN
  16.      WriteLn(Usage);
  17.      WriteLn(Example);
  18.      CASE ErrCode OF
  19.           1 : Message := 'Incorrect number of parameters on command line.';
  20.           2 : Message := 'Invalid switch on command line.';
  21.           3 : Message := 'Cannot open input file '+ IFL + '.';
  22.           4 : Message := 'Cannot open NEW output file '+ OFL + '.';
  23.           5 : Message := 'Non-numeric '+ LPP + ' specified for lines per page.';
  24.      ELSE
  25.           Message := 'Unknown error.';
  26.      END;
  27.      Writeln(Message);
  28.      Halt;
  29. END;
  30.  
  31. PROCEDURE ParseComLine;
  32. CONST
  33.      Bad = '';   {An invalid filename character & invalid integer.}
  34. VAR
  35.      SwStr            : String[1];
  36.      SwChar           : Char;
  37.      ic               : Byte;
  38.      ArgText          : String[80];
  39. BEGIN
  40.      IF ParamCount <> 3 THEN
  41.         InValidParms(1);
  42.  
  43.      IFL := Bad; OFL := Bad; LPP := Bad;
  44.  
  45.      FOR ic := 1 to 3 DO
  46.      BEGIN
  47.          SwStr  := Copy(ParamStr(ic),1,1);
  48.          SwChar := UpCase(SwStr[1]);
  49.          IF (SwChar <> '/') THEN
  50.             InValidParms(2);
  51.          SwStr := Copy(ParamStr(ic),2,1);
  52.          SwChar := UpCase(SwStr[1]);
  53.          ArgText := Copy(ParamStr(ic),3,(Length(ParamStr(ic))-2));
  54.          CASE SwChar OF
  55.               'I' : IFL := ArgText;
  56.               'O' : OFL := ArgText;
  57.               'L' : LPP := ArgText;
  58.          END;
  59.      END;
  60. END;
  61.  
  62. PROCEDURE Open_I_O_Files;
  63. BEGIN
  64.      Assign(InFile,IFL);
  65. {$I-} Reset(InFile); {$I+}
  66.      IF IOResult <> 0 THEN
  67.         InValidParms(3);
  68.  
  69.      Assign(OutFile,OFL);
  70. {$I-} Rewrite(OutFile);  {$I+}
  71.      IF IOResult <> 0 THEN
  72.         InValidParms(4);
  73. END;
  74.  
  75. PROCEDURE GetEvenOdd ( lpp_param : string; var oddnum, evennum : Word);
  76.                        { determine page length for odd/ even pages }
  77. VAR
  78.    odds, evens,        { string of odd/ even lines per page }
  79.    lstr  : string[5];  { entire string containing numbers needed }
  80.  
  81.    vlppo,vlppe,        { numeric of lines per page odd/even }
  82.    lval  : byte;       { numeric of string containing numbers needed }
  83.  
  84.    pcode : integer; { error code, will be non-zero if strings are not numbers }
  85.  
  86. BEGIN
  87.      lstr := lpp_param;
  88.  
  89.      IF ((pos(':',lstr)) <> 0) THEN
  90.      BEGIN                                            { determine position of }
  91.         odds  := copy(lstr,1,((pos(':',lstr))-1));    { any colon, and divide }
  92.         evens := copy(lstr,((pos(':',lstr))+1),length(lstr)); { at that point }
  93.  
  94.         val(odds,vlppo,pcode);       { convert first part of string         }
  95.         IF (pcode <> 0) THEN         { into numeric                         }
  96.            InValidParms(5);          { show help if any errors              }
  97.  
  98.         val(evens,vlppe,pcode);      { convert first part of string         }
  99.         IF (pcode <> 0) THEN         { into numeric                         }
  100.            InValidParms(5);          { show help if any errors              }
  101.      END
  102.  
  103.      ELSE BEGIN  { if colon not present, lines/pg should be entire parameter }
  104.         val(lstr,lval,pcode);        { convert entire of string             }
  105.         IF (pcode <> 0) THEN         { into numeric                         }
  106.            InValidParms(5);          { show help if any errors              }
  107.         vlppo := lval;
  108.         vlppe := lval;
  109.      END;
  110.      oddnum  := vlppo;
  111.      evennum := vlppe;
  112. END;
  113.  
  114. PROCEDURE InsertFF( OddLines, EvenLines : Word);
  115. CONST
  116.      FF = ' ';
  117. VAR
  118.      LinesCopied,
  119.      LinesPerPage,
  120.      PageCopying   : Word;
  121.      ALine         : String;
  122. BEGIN
  123.      LinesCopied := 0;
  124.      LinesPerPage := OddLines;
  125.      PageCopying := 1;
  126.      WHILE (NOT Eof(InFile)) DO
  127.      BEGIN
  128.           ReadLn(InFile,ALine);
  129.           IF (LinesCopied = LinesPerPage) THEN
  130.           BEGIN
  131.              ALine := FF + ALine;
  132.              LinesCopied := 0;
  133.              PageCopying := Succ(PageCopying);
  134.              IF ((PageCopying MOD 2) = 0) THEN
  135.                 LinesPerPage := EvenLines
  136.              ELSE
  137.                 LinesPerPage := OddLines;
  138.           END;
  139.           WriteLn(OutFile,ALine);
  140.           LinesCopied := Succ(LinesCopied);
  141.      END;
  142. END;
  143.  
  144. BEGIN                                { main }
  145.      Writeln(ProgData);
  146.      Writeln(ProgDat2);
  147.      Writeln;
  148.      ParseComLine;
  149.      Open_I_O_Files;
  150.      GetEvenOdd(LPP,OddL,EvenL);
  151.      Writeln('Input file: ',IFL,' - Output file: ',OFL,'.');
  152.      Writeln('Lines per odd / even page: ',Oddl,' / ',EvenL,'.');
  153.      InsertFF(OddL,EvenL);
  154.      Close(InFile);
  155.      Close(OutFile);
  156.      Writeln('Done!');
  157. END.
  158.